package com.chenfan.process.entity.po;

import com.baomidou.mybatisplus.annotation.TableName;
import com.chenfan.ccp.entity.BaseEntity;
import com.chenfan.ccp.util.tool.UuidUtil;
import com.chenfan.infra.context.UserContextHolder;
import com.chenfan.process.entity.vo.ProcessRelatedVo;
import com.chenfan.process.enums.LinkedTypeEnums;
import com.chenfan.process.enums.ProcessRelatedTypeEnums;
import com.chenfan.process.enums.ProcessResultTypeEnums;
import com.chenfan.process.util.TenantUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

import java.util.Date;

/**
 * @author K
 * @date 2021/3/4
 */
@EqualsAndHashCode(callSuper = true)
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Data
@TableName(value = "chenfan_paas_process_related_po")
public class ProcessRelatedPo extends BaseEntity<ProcessRelatedPo> {

    public static final String APPROVAL_TEMPLATE_ID = "approval_template_id";
    public static final String PROCESS_BEFORE_ID = "process_before_id";
    public static final String PROCESS_STATUS = "process_status";
    public static final String VERSION = "version";
    public static final String GROUP_ID = "group_id";

    @ApiModelProperty("上一个节点")
    private Long processBeforeId;

    @ApiModelProperty("下一个节点")
    private Long processAfterId;

    @ApiModelProperty("分组id, 随机生成")
    private Long groupId;

    @ApiModelProperty("类型 0-串行 1-并行")
    private Integer type;

    /**
     * 第一个创建的定位 create,其余正常为running,仅仅作为标识，不做其他使用
     * <p>
     * 参考 {@link ProcessRelatedTypeEnums}
     */
    @ApiModelProperty("执行的状态,参考 {@link com.chenfan.process.enums.ProcessRelatedType}")
    private Integer processStatus;

    @ApiModelProperty("审批模板ID")
    private Long approvalTemplateId;

    @ApiModelProperty("关联类型 关联类型 0-普通节点关系 1-或签条件关系 2-条件分支 3-会签条件")
    private Integer linkType;

    @ApiModelProperty("版本")
    private Long version;

    /**
     * 租户id
     * @return
     */
    private Long tenantId;

    public boolean orSign() {
        return LinkedTypeEnums.PARALLEL.getCode().equals(this.getType()) || LinkedTypeEnums.PARALLEL.getCode().equals(this.getLinkType());
    }

    public boolean counterSign() {
        return LinkedTypeEnums.SERIAL.getCode().equals(this.getType());
    }

    public static ProcessRelatedPo ofDefault(Long before, Long after) {
        ProcessRelatedPo processRelatedPo = new ProcessRelatedPo();
        processRelatedPo.setId(UuidUtil.generateId());
        processRelatedPo.setProcessStatus(ProcessResultTypeEnums.CREATED.getIndex());
        processRelatedPo.setProcessBeforeId(before);
        processRelatedPo.setProcessAfterId(after);
        processRelatedPo.setTenantId(TenantUtil.getTenantId());
        return processRelatedPo;
    }

    public ProcessRelatedVo copyProcessRelatedVo() {
        ProcessRelatedVo vo = new ProcessRelatedVo();
        org.springframework.beans.BeanUtils.copyProperties(this, vo);
        return vo;
    }

    public ProcessRelatedPo cloneProcessRelatedPo() {
        ProcessRelatedPo processRelatedPo = new ProcessRelatedPo();
        org.springframework.beans.BeanUtils.copyProperties(this, processRelatedPo);
        return processRelatedPo;

    }
}
